package com.happystreet.engine
{
	import com.happystreet.GlobalGameConstants;
	import com.happystreet.config.GameConfig;
	import com.happystreet.config.ItemsConfig;
	import com.happystreet.info.BackgroundInfo;
	import com.happystreet.info.BackgroundLayerInfo;
	import com.happystreet.manager.GameManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 街道背景类
	 * @author Liseen
	 * 
	 */	
	public class StreetBackground extends CoreObject
	{
		
		public static const BACKGROUND_LENGTH:Number = 1000;
		public static const BACKGROUND_Y:Number = 480;
		
		public static var instance:StreetBackground;
		public var layersList:Array;
		public var depthsList:Array;
		public var widthsList:Array;
		public var heigthsList:Array;
		
		private var _gameManager:GameManager;
		public function StreetBackground()
		{
			super();
			instance = this;
			
			_gameManager = GameManager.getInstance();
			_gameManager.addEventListener(GameManager.CHANGE_STREET_BACKGROUND,this.onChangeBackGround,false,0,true);
			layersList = new Array();
			this.depthsList = new Array();
			this.widthsList = new Array();
			this.heigthsList = new Array();
			
		}
		
		/**
		 *更换背景 
		 * @param event
		 * 
		 */		
		private function onChangeBackGround(event:Event):void
		{
			buildBackground(GameConfig.workingUserBackground);
		}
		/**
		 *根据背景道具id设置背景 
		 * @param id
		 * 
		 */		
		public function buildBackground(id:int):void
		{
			var backGroundLayerInfo:BackgroundLayerInfo;
			id = (id!=0) ? id : GlobalGameConstants.DEFAULT_STREET_BACKGROUND;
			//清空
			while(layersList.length>0)
			{
				this.removeChild(layersList.shift());
			}
			var backGroundInfo:BackgroundInfo = BackgroundInfo(ItemsConfig.getItemInfo(id));
			
			
			for each(backGroundLayerInfo in backGroundInfo.layerTable)
			{
				switch(backGroundLayerInfo.type){
					case GlobalGameConstants.BG_LAYER_SINGLE_TYPE:
						this.addSingleLayer(backGroundLayerInfo.symbolName, backGroundLayerInfo.width, backGroundLayerInfo.height);
						break;
					case GlobalGameConstants.BG_LAYER_PATTERN_TYPE:
						this.addLayer(backGroundLayerInfo.symbolName, backGroundLayerInfo.depth, backGroundLayerInfo.height);
						break;
					case GlobalGameConstants.BG_LAYER_RANDOM_TYPE:
						this.addCloudsLayer(backGroundLayerInfo.symbolName, backGroundLayerInfo.depth, backGroundLayerInfo.number, backGroundLayerInfo.height);
						break;
					
				}
			}
		}
		/**
		 *创建简单背景 
		 * @param sName
		 * @param width
		 * @param height
		 * 
		 */		
		public function addSingleLayer(sName:String,width:Number,height:Number):void
		{
			var layer:Sprite = new Sprite();
			layer.x = width;
			layer.y = height;
			var backGround:CoreObject = new CoreObject(sName);
			layer.addChild(backGround);
			this.layersList.push(layer);
			this.depthsList.push(0);
			this.widthsList.push(backGround.width);
			this.heigthsList.push(height);
			addChild(layer);	
		}
		/**
		 *添加图案类背景 
		 * @param sName
		 * @param depth
		 * @param height
		 * 
		 */		
		public function addLayer(sName:String, depth:Number, height:Number):void
		{
			var layer:Sprite = new Sprite();
			layer.y = height;
			var backGround:CoreObject = new CoreObject(sName);
			var len:int = Math.max(Math.ceil(BACKGROUND_LENGTH / backGround.width) + 1, 3);
			var index:int;
			while (index < len)
			{
				backGround = new CoreObject(sName);
				backGround.alignTo((backGround.width * index), 0, CoreObject.ALIGN_RIGHT);
				layer.addChild(backGround);
				index++;
			}
			this.layersList.push(layer);
			this.depthsList.push(depth);
			this.widthsList.push(backGround.width);
			this.heigthsList.push(height);
			addChild(layer);
		}
		
		/**
		 *添加云彩类背景 
		 * @param sName
		 * @param depth
		 * @param num
		 * @param height
		 * 
		 */		
		public function addCloudsLayer(sName:String, depth:Number, num:int, height:int=0):void
		{
			
		}
	}
}